﻿
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.Domains.SheepManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.Breedingsettinges
{
    public class BreedingsettingQueryHandler : IRequestHandler<BreedingsettingQueryCommand, ResultPage<BreedingsettingQueryDto>>
    {
        private readonly IBaseRepository<Breedingsetting> _breedingsettingRepository;
        private readonly IBaseRepository<SheepTransfer> _sheepTransferRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieIdRepository;

        public BreedingsettingQueryHandler(IBaseRepository<Breedingsetting> breedingsettingRepository, IBaseRepository<SheepTransfer> sheepTransferRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieIdRepository)
        {
            _breedingsettingRepository = breedingsettingRepository;
            _sheepTransferRepository = sheepTransferRepository;
            _cottageRepository = cottageRepository;
            _fieIdRepository = fieIdRepository;
        }




        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<BreedingsettingQueryDto>> Handle(BreedingsettingQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<BreedingsettingQueryDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var breedingsettingList = await (from a in await _breedingsettingRepository.GetAll()
                                                 join b in await _cottageRepository.GetAll() on a.CottageId equals b.CottageId
                                                 join c in await _fieIdRepository.GetAll() on a.Field equals c.FieldId
                                                 where a.IsDeleted == false
                                                 select new BreedingsettingQueryDto()
                                                 {
                                                     BreedingsettingId=a.BreedingsettingId,
                                                     RecordAuricle  = a.RecordAuricle,
                                                     StudRAMS =a.StudRAMS,
                                                     Breedingpattern = a.Breedingpattern,
                                                     Estrusspecies = a.Estrusspecies,
                                                     CottageId = a.CottageId,
                                                     CottageName = b.CottageName,//转入栋舍
                                                     Field = a.Field,
                                                     FieldName =c.FieldName,//转入栏位
                                                     Remark = a.Remark,
                                                     SheepId = a.SheepId,
                                                     BreedingsettingTime = a.BreedingsettingTime,
                                                     CreatedOwnerId = a.CreatedOwnerId,
                                                     CreatedTime = a.CreatedTime,
                                                     CurrentCottageName = a.CurrentCottageName,//当前栋舍
                                                     CurrentFieIdName = a.CurrentFieIdName,//当前栏位
                                                 }).ToListAsync();
                if (request.StartBreedingsettingTime != null)
                {
                    breedingsettingList = breedingsettingList.Where(c => c.BreedingsettingTime >= request.StartBreedingsettingTime).ToList();
                }
                if (request.EndBreedingsettingTime != null)
                {
                    breedingsettingList = breedingsettingList.Where(c => c.BreedingsettingTime <= request.EndBreedingsettingTime).ToList();
                }
                result.TotalCount = breedingsettingList.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                breedingsettingList = breedingsettingList.OrderBy(c => c.BreedingsettingId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = breedingsettingList;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
